//登录
function login()
  	{
  		var password=$("#password").val();
  		var username=$("#username").val();
  		var data={"username":username,"password":password};
  		$.ajax({
		  url: "login",
		  type:"post",
		  dataType:"json",	
		  data:data,	  
		  success: function(data){
		  	
		  	if(data==2)
		  	{
		  		alert("用户名不存在！");
		  	}else if(data==1)
		  	{
		  		alert("登录成功！");
		  		
		  		$("#login").hide();
		  		$("#search").show();
		  		$("#solist").show();
		  		$("#guiji").show();
		  		
		  		
		  	}
		  	else if(data==3)
		  	{
		  		alert("密码错误！");
		  	}else if(data==4)
		  	{
		  		alert("程序异常！");
		  	}
		  }
		  });
  	
  	}
 //查询所有
 function getList()
{
  		$.ajax({
		  url: "sosList",
		  dataType:"json",		  
		  success: function(data){
		  	initTable(data);
		  	
		    
		  }
		});
}

function getInfo()
{
	$.ajax({
		  url: "getinfo",
		  dataType:"json",		  
		  success: function(data){
		  	alert(data);
		  	
		    
		  }
		});
}
//
function initialize(lat,lon,info) {
			alert("map");
			var map = new BMap.Map('map');
			//左标
			var point = new BMap.Point(lon,lat); 
			map.centerAndZoom(point, 19);	
			//标注
			var marker = new BMap.Marker(point);  
			map.addOverlay(marker);   
			//
			marker.addEventListener("click", function(e){  
			 alert("hello!");  
			 alert("当前位置：" + e.point.lng + ", " + e.point.lat);
			 map.openInfoWindow(infoWindow, map.getCenter());
			});
			//窗口
			var opts = {  
			 width : 250,     // 信息窗口宽度  
			 height: 100,     // 信息窗口高度  
			 title : "求救"  // 信息窗口标题  
			}  
			var infoWindow = new BMap.InfoWindow(info, opts);  // 创建信息窗口对象  
			map.openInfoWindow(infoWindow, map.getCenter());      // 打开信息窗口
			//画线条
			var polyline = new BMap.Polyline([  
			   new BMap.Point(113.3572672,23.1568597),  
			   new BMap.Point(113.3472672,23.1468597)  
			 ],  
			 {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5}  
			);  
			//map.addOverlay(polyline);
			//添加地图控件
			map.addControl(new BMap.NavigationControl());  
	 }
	 
//
function getLocations()
{
		var polyline = new BMap.Polyline({strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
		//polyline.setStrokeColor({strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
		var pointarray=new Array();
		$.ajax({
		  url: "locationList",
		  dataType:"json",	
		  data:{"phoneId":"460008871850429"},	  
		  success: function(data)
		 {
		  	//alert(data);
		  	var list=data[0];
		    for(i=0;i<list.length;i++)
		  	{
		  		var loc=list[i];
		  		var point =new BMap.Point(loc.lat,loc.lon); 
		  		pointarray[i]=point;
		  		// polyline.setPositionAt(i, point) ;
		  	}
		  //	alert(pointarray);
			polyline.setPath(pointarray) ;
			
			//alert(polyline.getPath());
		
		  }
		});
		
		
}
//画线
function mapLine(phoneid)
{
	    var map = new BMap.Map('map2');
		map.removeOverlay();
		//左标
		var point = new BMap.Point(113.3572672,23.1568597); 
		map.centerAndZoom(point, 14);	
		//标注
		var marker = new BMap.Marker(point);  
		map.addOverlay(marker);   
		
		
		
		//画线条
		/****
		var polyline = new BMap.Polyline([  
		   new BMap.Point(113.3572672,23.1568597),  
		   new BMap.Point(113.3472672,23.1468597)  
		 ],  
		 {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5}  
		);  
		*****/
		var polyline = new BMap.Polyline({strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
		//polyline.setStrokeColor({strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
		var pointarray=new Array();
		$.ajax({
		  url: "locationList",
		  dataType:"json",	
		  data:{"phoneId":phoneid},	  
		  success: function(data)
		 {
		  	//alert(data);
		  	var list=data[0];
		    for(i=0;i<list.length;i++)
		  	{
		  		var loc=list[i];
		  		var point =new BMap.Point(loc.lon,loc.lat); 
		  		pointarray[i]=point;
		  		// polyline.setPositionAt(i, point) ;
		  	}
		  //	alert(pointarray);
			polyline.setPath(pointarray) ;
			
			//alert(polyline.getPath());
			//alert(polyline.getPath());
			map.addOverlay(polyline);
		  }
		});
		
		//var polyline =getLocations();
		
		//添加地图控件
		map.addControl(new BMap.NavigationControl());  
}
 //根据号码查询
function getByPhone()
  	{
  		var phone=$("#phone").val();
  		$.ajax({
		  url: "selectByPhone",
		  data:{"phone":phone},
		  type:"post",
		  dataType:"json",		  
		  success: function(data){
		  	initTable(data);
		  	
		    
		  }
		});
 }
 function initTable(data)
  	{
  			var list=data[0];
		  	var table="";
		  	var tr="<tr><td>电话</td><td>姓名</td><td>地址</td><td>经度</td><td>纬度</td><td>时间</td><td>电话ID</td></tr>";
		  	for(i=0;i<list.length;i++)
		  	{
		  	    var sosinfo=list[i];
		  		
		  		tr=tr+"<tr >"+
		  				"<td><a href=getinfo?id="+sosinfo.soId+" >"+sosinfo.soUserPhone+"</a></td>"+
		  				"<td onclick=initialize("+sosinfo.soLat+","+sosinfo.soLon+",'"+sosinfo.soName+"') >"+sosinfo.soName+"</td>"+
		  				
		  				"<td>"+sosinfo.soAddress+"</td>"+
		  				"<td>"+sosinfo.soLat+"</td>"+
		  				"<td>"+sosinfo.soLon+"</td>"+
		  				
		  				"<td>"+sosinfo.soTime+"</td>"+
		  				"<td><a onclick=mapLine("+sosinfo.soPhoneId+")>"+sosinfo.soPhoneId+"</a></td>"+
		  				
		  				
		  				"</tr>"
		  				;
		  		//alert(sosinfo.soId);
		  	}
		  	$("#solist").html(tr);
  	}